package dao.Impl;
import dao.BaseDao;
import dao.UserDao;
import entity.User;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public int add(User user) {
        String SQL="insert into cb_user value(default,?,?,default)";
        int count=0;
        try {
            super.open_db();
            super.sta=conn.prepareStatement(SQL);
            sta.setString(1,user.getName());
            sta.setString(2,user.getPasword());
            count=sta.executeUpdate();
        } catch (SQLException e) {
            count=0;
            throw new RuntimeException(e);
        } finally {
            super.close_db();
        }
        return count;
    }

    @Override
    public int modify(User user) {
        return 0;
    }

    @Override
    public int remove(User user) {
        return 0;
    }

    @Override
    public List<User> findAll() {
        return null;
    }

    @Override
    public List<User> findbyId(Object o) {
        List<User> list=new ArrayList<>();
        User use=(User) o;
     String SQL="select * from cb_user where name=?";

        try {
           super.open_db();
           sta=conn.prepareStatement(SQL);
           sta.setString(1,use.getName());
           rs=sta.executeQuery();
           while (rs.next()){
               use.setId(rs.getInt("id"));
               use.setNumber(rs.getInt("score"));
               list.add(use);
           }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
        }
         return list;

    }

    @Override
    public int query(User user) {
        return 0;
    }


    @Override
    public User Login(User use) {
     String SQL="select * from cb_user where name=? and password=?";
        try {
           super.open_db();
           sta=conn.prepareStatement(SQL);
           sta.setString(1,use.getName());
           sta.setString(2,use.getPasword());
           rs=sta.executeQuery();
           while (rs.next()){
               use.setId(rs.getInt("id"));
               use.setNumber(rs.getInt("score"));
           }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            super.close_db();
        }
         return use;
    }


    @Override
    public List<User> query(Map<String, Object> list) {
        return null;
    }
}
